#include "MCCommandManager.h"
#include "MCCommand.h"
#include <ICanvas.h>
#include <iostream>

MCCommandManager::MCCommandManager(ICanvas *presenter) :
    _presenter(presenter),
    _currentCommand(nullptr)
{
}

MCCommandManager::~MCCommandManager()
{
    if (_currentCommand != nullptr)
    {
        delete _currentCommand;
    }
}

ICanvas *MCCommandManager::getPresenter()
{
    return _presenter;
}

void MCCommandManager::setCommand(MCCommand *command)
{
    if (_currentCommand != nullptr)
    {
        _currentCommand->terminate();
        delete _currentCommand;
    }

    _currentCommand = command;
}

void MCCommandManager::finishCurrentCommand()
{
    if (_currentCommand != nullptr)
    {
        delete _currentCommand;
        _currentCommand = nullptr;
        _presenter->resetGrips();
    }
}

bool MCCommandManager::hasCommand()
{
    return _currentCommand != nullptr;
}

void MCCommandManager::onMouseDown(IMouseArgs &args)
{
    if (hasCommand())
        _currentCommand->onMouseDown(args);
}

void MCCommandManager::onMouseUp(IMouseArgs &args)
{
    if (hasCommand())
        _currentCommand->onMouseUp(args);
}

void MCCommandManager::onMouseMove(IMouseArgs &args)
{
    if (hasCommand())
        _currentCommand->onMouseMove(args);
}

void MCCommandManager::onKeyDown(IKeyArgs &args)
{
    if (hasCommand())
        _currentCommand->onKeyDown(args);
}
